package com.czhoujunlv.weather;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.opengl.GLDebugHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class DBmanage {
    public static SQLiteDatabase database;

    public static void initDB(@Nullable Context context){
        City_SQLite city_sqLite = new City_SQLite(context,"city.db",null,1);
        database = city_sqLite.getWritableDatabase();
    }
    /**
     * 查找数据库中的城市
     * */
    public static List<Sql_bean> queryAllCity(){//查找城市
        Cursor cursor = database.query("citys",null,null,null,null,null,null);
        List<Sql_bean> cityList = new ArrayList<>();

        while(cursor.moveToNext()){
            Sql_bean sql_bean = new Sql_bean();
            String city = cursor.getString(cursor.getColumnIndex("city_name"));
            int low = cursor.getInt(cursor.getColumnIndex("low_temp"));
            int high = cursor.getInt(cursor.getColumnIndex("high_temp"));
            int now = cursor.getInt(cursor.getColumnIndex("now_temp"));
            Log.i("现有城市：",city);
            sql_bean.Sql_bean(city,low,high,now);
            Log.i("添加的城市：",sql_bean.getCity_name());
            cityList.add(sql_bean);
        }
        return cityList;
    }
    public static List<String> queryAllCityName(){//查找城市名
        Cursor cursor = database.query("citys",null,null,null,null,null,null);
        List<String> cityList = new ArrayList<>();

        while(cursor.moveToNext()){

            String city = cursor.getString(cursor.getColumnIndex("city_name"));
            cityList.add(city);
        }
        return cityList;
    }
    /**
     * 向数据库中添加数据
     ***/
    public static long addCity(String city_name , int low_temp , int high_temp,int now_temp){
        ContentValues contentValues = new ContentValues();
        contentValues.put("city_name",city_name);
        contentValues.put("low_temp",low_temp);
        contentValues.put("high_temp",high_temp);
        contentValues.put("now_temp",now_temp);
        return database.insert("citys",null,contentValues);
    }
    /**
     * 更新数据，主要用于，检查是否有数据更新
     * */
    public static int updateCity(String city,String s){
        ContentValues contentValues = new ContentValues();
        contentValues.put("city_name",city);
//        contentValues.put("low_temp",low);
//        contentValues.put("high_temp",high);
//        contentValues.put("mow_temp",now);
        return database.update("citys",contentValues,"city_name=?",new String[]{city});

    }
    /**
     * 删除数据
     * */
    public static int deletecity(String cityname){
        return database.delete("citys","city_name=?",new String[]{cityname});
    }
}
